#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: api08_worker.py 
@version:
@time: 2022/05/05 
"""
from TapClientCore.T02_tm_api.v2_api.v2_api_base import *


class Api09ModulesUrl(V2ApiUrlBase):
    url_modules = api_joint(V2ApiUrlBase().server, MODULES)
    url_modules_preview = api_joint(url_modules, MODULES_PREVIEW)
    url_modules_rankLists = api_joint(url_modules, MODULES_RANKLISTS)
    url_modules_apiList = api_joint(url_modules, MODULES_APILIST)


class Api09Modules(V2ApiBase):

    def get_modules_by_filter(self, _filter):
        """
        通过filter获取发布API信息
        """
        response = self.tapclient(
            self.get,
            Api09ModulesUrl.url_modules
            + self.access_token
            + self.filter(_filter)
        )
        return response

    def post_modules(self, payload):
        response = self.tapclient(
            self.post,
            Api09ModulesUrl.url_modules
            + self.access_token,
            json=payload
        )
        return response

    def patch_modules(self, payload):
        """
        通过id字段去patch
        {
          "status": "active",
          "id": "626254f8ae753717df0a72cd",
          "tablename": "my2"
        }
        取消发布：pending
        """
        response = self.tapclient(
            self.patch,
            Api09ModulesUrl.url_modules
            + self.access_token,
            json=payload
        )
        return response

    def delete_modules_by_id(self, modules_id, table_name):
        response = self.tapclient(
            self.delete,
            api_joint(
                Api09ModulesUrl.url_modules,
                modules_id,
            )
            + f'?tablename={table_name}'
            + self.access_token2
        )
        return response

    def post_modules_copy_by_id(self, modules_id, payload):
        """
        copy modules
        {
          "uri": "625ceb57ab0ea359f9e9be15/copy",
          "headers": {
            "lconname-name": "test11"
          }
        }
        """
        response = self.tapclient(
            self.post,
            api_joint(
                Api09ModulesUrl.url_modules,
                modules_id,
                'copy'
            )
            + self.access_token,
            json=payload
        )
        return response

    def get_modules_preview(self):
        response = self.tapclient(
            self.get,
            Api09ModulesUrl.url_modules_preview
            + self.access_token
        )
        return response

    def get_modules_rankLists_by_filter(self, _filter):
        response = self.tapclient(
            self.get,
            Api09ModulesUrl.url_modules_rankLists
            + self.access_token
            + self.filter(_filter)
        )
        return response

    def get_modules_apiList(self):
        response = self.tapclient(
            self.get,
            Api09ModulesUrl.url_modules_apiList
            + self.access_token
        )
        return response
